﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace TracyGUI.Model
{
    static class ZipHelper
    {
        public static void ZipXml(XmlDocument doc, string fileName)
        {
            ZipOutputStream s = new ZipOutputStream(new FileStream(fileName, FileMode.Create));
            try
            {
                s.SetLevel(9);
                ZipEntry ze = new ZipEntry("ExportedEntries.xml");
                ze.DateTime = DateTime.Now;
                s.PutNextEntry(ze);
                doc.Save(s);
                s.Finish();
            }
            finally
            {
                s.Close();
            }
        }

        public static XmlDocument UnzipXml(string fileName)
        {
            if (File.Exists(fileName))
            {
                XmlDocument doc = new XmlDocument();
                ZipInputStream s = new ZipInputStream(File.OpenRead(fileName));
                try
                {
                    ZipEntry ze = s.GetNextEntry();

                    if (ze != null)
                    {
                        if (Path.GetFileName(ze.Name) != "ExportedEntries.xml") return null;
                        doc.Load(s);
                    }
                }
                finally
                {
                    s.Close();
                }
                return doc;
            }
            return null;
        }
    }
}
